import { useQuery, useMutation, UseQueryOptions, UseMutationOptions } from '@tanstack/react-query';
import { api } from '@/services/api';
import { ApiError } from '@/types/api';

// GET 请求 Hook
export function useGet<TData = unknown, TError = ApiError>(
  key: string[],
  url: string,
  options?: UseQueryOptions<TData, TError>
) {
  return useQuery<TData, TError>({
    queryKey: key,
    queryFn: () => api.get<TData>(url),
    ...options,
  });
}

// POST 请求 Hook
export function usePost<TData = unknown, TVariables = unknown, TError = ApiError>(
  url: string,
  options?: UseMutationOptions<TData, TError, TVariables>
) {
  return useMutation<TData, TError, TVariables>({
    mutationFn: (variables) => api.post<TData>(url, variables),
    ...options,
  });
}

// PUT 请求 Hook
export function usePut<TData = unknown, TVariables = unknown, TError = ApiError>(
  url: string,
  options?: UseMutationOptions<TData, TError, TVariables>
) {
  return useMutation<TData, TError, TVariables>({
    mutationFn: (variables) => api.put<TData>(url, variables),
    ...options,
  });
}

// DELETE 请求 Hook
export function useDelete<TData = unknown, TError = ApiError>(
  url: string,
  options?: UseMutationOptions<TData, TError, void>
) {
  return useMutation<TData, TError, void>({
    mutationFn: () => api.delete<TData>(url),
    ...options,
  });
} 